#include "StdAfx.h"
#include "GoogleEntry.h"

namespace GoogleProtocol
{

CGoogleEntry* CGoogleEntry::m_pUniqueInst = NULL;

CGoogleEntry::CGoogleEntry(void)
{
}

CGoogleEntry::~CGoogleEntry(void)
{
}

//-------------------------------------------------------------------
//  Description:      Get Google Entry Instance.
//
//  Return:
//                    Unique Instance.
//-------------------------------------------------------------------
CGoogleEntry* CGoogleEntry::GetInstance()
{
	if (m_pUniqueInst == NULL)
	{
		m_pUniqueInst = new CGoogleEntry();
	}

	return m_pUniqueInst;
}

//-------------------------------------------------------------------
//  Description:      Free Google Entry Instance.
//
//  Return:
//                    Unique Instance.
//-------------------------------------------------------------------
void CGoogleEntry::FreeInstance()
{
	if (m_pUniqueInst != NULL)
	{
		delete m_pUniqueInst;
		m_pUniqueInst = NULL;
	}
}

//-------------------------------------------------------------------
//  Description:      Login to google server.
//
//  Parameter:
//      lpIPAddr:     Receives server' ip address.
//      lpUserName:   User Account.
//      lpPassword:        User Password.
//
//  Return:
//       1            Succeed.
//       0            Failed.
//      -1            Http request error;
//      -2            Authorization error.
//      -3            Account Id or password error.
//-------------------------------------------------------------------
LRESULT CGoogleEntry::LogIn(LPCSTR lpUserName, LPCSTR lpPassword)
{
	BOOL bUsedLocalProfile;

	bUsedLocalProfile = BOOL(lpUserName == NULL || lpPassword == NULL);

	if (bUsedLocalProfile)
	{
		m_userdata.LoadUserData();
		lpUserName = m_userdata.GetUserName();
		lpPassword = m_userdata.GetUserPwd();
	}

	m_userdata.SetUserName(lpUserName);
	m_userdata.SetUserPwd(lpPassword);

	m_contact.SetUserData(&m_userdata);
	LRESULT lResult;

 	CGoogleAuth* auth = m_userdata.GetAuthToken();
	if (auth==NULL)
		lResult = G_AUTH_ERROR;
	CHECK_RESULT(BOOL(auth != NULL));

	lResult = m_contact.Logon(lpUserName, lpPassword, auth);
	CHECK_RESULT(lResult);

	if (!bUsedLocalProfile)
	{
		m_userdata.SaveUserData();
	}

RESULT:
	return lResult;
}

//-------------------------------------------------------------------
//  Description:     Logout from google server.
//
//  Parameter:
//
//  Return:
//      1            Succeed.
//      0            Failed.
//-------------------------------------------------------------------
LRESULT CGoogleEntry::LogOut()
{
	LRESULT lResult;
	lResult = m_contact.LogOut() ? 1 : 0;
	CHECK_RESULT(lResult);

RESULT:
	return lResult;
}

//-------------------------------------------------------------------
//  Description:      Set server ip address to google contact.
//
//  Parameter:
//      lpEntryName:  Server's name.
//      lpIPAddr:     New server' ip address.
//
//  Return:
//       2            Added succeed.
//       1            Update succeed.
//       0            Failed.
//      -1            Http request error;
//      -2            Authorization error.
//      -3            Account Id or password error.
//-------------------------------------------------------------------
LRESULT CGoogleEntry::SetIP(LPCSTR lpEntryName, LPCSTR lpIPAddr)
{
	_ASSERT(lpEntryName != NULL);
	_ASSERT(lpIPAddr != NULL);

	list<CGoogleContact::GoogleContact *>::iterator iter;
	list<CGoogleContact::GoogleContact *> lstContact;
	LRESULT lResult;
	bool bfound = false;

	lResult = m_contact.GetAllContact(lstContact);
	CHECK_RESULT(lResult);

	for (iter = lstContact.begin(); iter != lstContact.end(); iter++)
	{
		CGoogleContact::GoogleContact* contact = *iter;

		if (contact->IsContactAuther())
			continue;

		CGoogleContact::ContactEntry* entry = (CGoogleContact::ContactEntry *)(contact);
		_ASSERT(entry != NULL);

		if (entry->data.title.compare(lpEntryName) == 0)
		{
			entry->data.home_email = lpIPAddr;

			// update it
			lResult = m_contact.UpdateContact(*entry);
			CHECK_RESULT(lResult);
			bfound = true;
			break;
		}
	}

	// rlease memory
	for (iter = lstContact.begin(); iter != lstContact.end(); iter++)
	{
		CGoogleContact::GoogleContact* contact = *iter;
		if (contact != NULL) delete contact;
	}

	if (!bfound)
	{
		CGoogleContact::ContactEntry entry;
		entry.data.title = lpEntryName;
		entry.data.home_email = lpIPAddr;
		lResult = m_contact.CreateContact(entry);
		if (lResult == 1) lResult = 2; // add
		CHECK_RESULT(lResult);
		return lResult;
	}

RESULT:
	return lResult;
}

//-------------------------------------------------------------------
//  Description:      Get server ip address from google contact.
//
//  Parameter:
//      lpEntryName:  Server's name.
//      lpIPAddr:     New server' ip address.
//
//  Return:
//       2            Added succeed.
//       1            Update succeed.
//       0            Failed.
//      -1            Http request error;
//      -2            Authorization error.
//      -3            Account Id or password error.
//-------------------------------------------------------------------
LRESULT CGoogleEntry::GetIP(LPCSTR lpEntryName, LPSTR lpIPAddr)
{
	_ASSERT(lpEntryName != NULL);
	_ASSERT(lpIPAddr != NULL);

	list<CGoogleContact::GoogleContact *>::iterator iter;
	list<CGoogleContact::GoogleContact *> lstContact;
	LRESULT lResult;
	bool bfound = false;

	lResult = m_contact.GetAllContact(lstContact);
	CHECK_RESULT(lResult);

	for (iter = lstContact.begin(); iter != lstContact.end(); iter++)
	{
		CGoogleContact::GoogleContact* contact = *iter;

		if (contact->IsContactAuther())
			continue;

		CGoogleContact::ContactEntry* entry = (CGoogleContact::ContactEntry *)(contact);
		_ASSERT(entry != NULL);

		if (entry->data.title.compare(lpEntryName) == 0)
		{
			strcpy(lpIPAddr, entry->data.home_address.c_str());
			bfound = true;
			break;
		}
	}

	// rlease memory
	for (iter = lstContact.begin(); iter != lstContact.end(); iter++)
	{
		CGoogleContact::GoogleContact* contact = *iter;
		if (contact != NULL) delete contact;
	}

	if (!bfound) lResult = 0;
	CHECK_RESULT(lResult);

RESULT:
	return lResult;
}

//-------------------------------------------------------------------
//  Description:     Get UserName
//
//  Parameter:
//                   [OUT] UserName
//  Return:
//      1            Succeed.
//      0            Failed.
//-------------------------------------------------------------------
LRESULT CGoogleEntry::GetUserName(LPWSTR lpUserName)
{
	int bytes = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_userdata.GetUserName(), -1,
		lpUserName, 255);
	return bytes != 0;
}

//-------------------------------------------------------------------
//  Description:     Get UserPassword
//
//  Parameter:
//                   [OUT] UserPwd
//  Return:
//      1            Succeed.
//      0            Failed.
//-------------------------------------------------------------------
LRESULT CGoogleEntry::GetUserPwd(LPWSTR lpUserPwd)
{
	int bytes = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_userdata.GetUserPwd(), -1,
		lpUserPwd, 255);
	return bytes != 0;
}

}